package com.fyzk.activity.lottery.jc.football.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.fyzk.activity.lottery.jc.football.JZBetContentUtil;
import com.fyzk.activity.lottery.jc.football.JZBetNumUtil;
import com.fyzk.activity.lottery.jc.football.guoguan.JC_GuoGuanDialog;
import com.fyzk.activity.zhifu.JCBasePayActivity;
import com.fyzk.interfaces.OnFootballPayTeamDeleteListener;
import com.fyzk.utils.LogUtils;
import com.tyjc.lottery.R;
import com.fyzk.net.Constants;
import com.fyzk.net.PublicMethod;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by miao on 2017/7/19.
 */

public class JCZQ_ZJQ_ZhiFuAdapter extends JCZQZhiFuBaseAdapter {

    private boolean isdanguan;
    private OnFootballPayTeamDeleteListener listener;
    private Context context;
    private int danCount = 0;
    private int zhuShu = 0;
    private Handler handler;

    @Override
    public void setInfors(List<JCInfo> infors, boolean isDanguan) {
        this.isdanguan = isDanguan;
        this.infors = getInfor(infors);
        this.notifyDataSetChanged();
        if(infors.size()==0){
            jiSuanZhuShu();
        }
    }

    public JCZQ_ZJQ_ZhiFuAdapter(Context context, boolean type, List<JCInfo> infors, Handler
            handler, OnFootballPayTeamDeleteListener listener) {
        this.isdanguan = type;
        this.infors = getInfor(infors);
        this.listener = listener;
        this.context = context;
        this.handler = handler;
        refreshDan();
    }

    /**
     * 获得支付list数据
     *
     * @param infors
     * @return
     */
    private List<JCInfo> getInfor(List<JCInfo> infors) {
        ArrayList<JCInfo> list = new ArrayList<>();
        for (int i = 0; i < infors.size(); i++) {
            JCInfo info = infors.get(i);
            if (info.getOnclikNum() > 0) {
                list.add(info);
            }
        }
        JCBasePayActivity.infos = list;
        return list;
    }


    @Override
    public int getCount() {
        return infors.size();
    }

    @Override
    public JCInfo getItem(int position) {
        return infors.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.jc_bet_zjq_item, null);
            holder.zhu = convertView.findViewById(R.id.jc_zhifu_list_item_zhu);
            holder.delete = convertView.findViewById(R.id.jc_zhifu_list_item_delete);
            holder.team_home = convertView.findViewById(R.id.jc_zhifu_list_item_tv_sheng1);
            holder.ping1 = convertView.findViewById(R.id.jc_zhifu_list_item_tv_ping1);
            holder.team_away = convertView.findViewById(R.id.jc_zhifu_list_item_tv_fu1);
            holder.week_num = convertView.findViewById(R.id.week_num);
            holder.score_0 = convertView.findViewById(R.id.zjq_num_0);
            holder.score_1 = convertView.findViewById(R.id.zjq_num_1);
            holder.score_2 = convertView.findViewById(R.id.zjq_num_2);
            holder.score_3 = convertView.findViewById(R.id.zjq_num_3);
            holder.score_4 = convertView.findViewById(R.id.zjq_num_4);
            holder.score_5 = convertView.findViewById(R.id.zjq_num_5);
            holder.score_6 = convertView.findViewById(R.id.zjq_num_6);
            holder.score_7 = convertView.findViewById(R.id.zjq_num_7);
            holder.dan = convertView.findViewById(R.id.jc_zhifu_list_item_dan);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        JCInfo infor = infors.get(position);
        if (isdanguan) {
            holder.dan.setVisibility(View.GONE);
        } else {
            holder.dan.setVisibility(View.VISIBLE);
            holder.dan.setTag(R.string.jc_info, infor);
            holder.dan.setChecked(infor.isDan());
            if (infor.isDaneble()) {
                holder.dan.setEnabled(true);
                holder.dan.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        JCInfo infor = (JCInfo) v.getTag(R.string.jc_info);
                        infor.setDan(!infor.isDan());
                        refreshDan();
                    }
                });
            } else {
                holder.dan.setEnabled(false);
            }
        }

        holder.zhu.setVisibility(View.VISIBLE);
        holder.team_home.setText(infor.getHomeTeam());
        holder.team_away.setText(infor.getAwayTeam());
        holder.ping1.setText("vs");
        holder.score_0.setText(getShowSP("0 " + infor.getScore_0()));
        holder.score_1.setText(getShowSP("1 " + infor.getScore_1()));
        holder.score_2.setText(getShowSP("2 " + infor.getScore_2()));
        holder.score_3.setText(getShowSP("3 " + infor.getScore_3()));
        holder.score_4.setText(getShowSP("4 " + infor.getScore_4()));
        holder.score_5.setText(getShowSP("5 " + infor.getScore_5()));
        holder.score_6.setText(getShowSP("6 " + infor.getScore_6()));
        holder.score_7.setText(getShowSP("7+ " + infor.getScore_7()));
        holder.week_num.setText(infor.getOfficialWeekDay() + PublicMethod.getTeamNum(infor.getMatchNum()));
        holder.score_0.setOnCheckedChangeListener(null);
        holder.score_1.setOnCheckedChangeListener(null);
        holder.score_2.setOnCheckedChangeListener(null);
        holder.score_3.setOnCheckedChangeListener(null);
        holder.score_4.setOnCheckedChangeListener(null);
        holder.score_5.setOnCheckedChangeListener(null);
        holder.score_6.setOnCheckedChangeListener(null);
        holder.score_7.setOnCheckedChangeListener(null);
        JCInfo.SPInfo spInfo_0 = infor.btnHGPojo.get(infor.SCORE_0);
        JCInfo.SPInfo spInfo_1 = infor.btnHGPojo.get(infor.SCORE_1);
        JCInfo.SPInfo spInfo_2 = infor.btnHGPojo.get(infor.SCORE_2);
        JCInfo.SPInfo spInfo_3 = infor.btnHGPojo.get(infor.SCORE_3);
        JCInfo.SPInfo spInfo_4 = infor.btnHGPojo.get(infor.SCORE_4);
        JCInfo.SPInfo spInfo_5 = infor.btnHGPojo.get(infor.SCORE_5);
        JCInfo.SPInfo spInfo_6 = infor.btnHGPojo.get(infor.SCORE_6);
        JCInfo.SPInfo spInfo_7 = infor.btnHGPojo.get(infor.SCORE_7);
        holder.score_0.setChecked(spInfo_0.isSel);
        holder.score_1.setChecked(spInfo_1.isSel);
        holder.score_2.setChecked(spInfo_2.isSel);
        holder.score_3.setChecked(spInfo_3.isSel);
        holder.score_4.setChecked(spInfo_4.isSel);
        holder.score_5.setChecked(spInfo_5.isSel);
        holder.score_6.setChecked(spInfo_6.isSel);
        holder.score_7.setChecked(spInfo_7.isSel);
        holder.score_0.setTag(spInfo_0);
        holder.score_1.setTag(spInfo_1);
        holder.score_2.setTag(spInfo_2);
        holder.score_3.setTag(spInfo_3);
        holder.score_4.setTag(spInfo_4);
        holder.score_5.setTag(spInfo_5);
        holder.score_6.setTag(spInfo_6);
        holder.score_7.setTag(spInfo_7);
        holder.score_0.setTag(R.string.jc_info, infor);
        holder.score_1.setTag(R.string.jc_info, infor);
        holder.score_2.setTag(R.string.jc_info, infor);
        holder.score_3.setTag(R.string.jc_info, infor);
        holder.score_4.setTag(R.string.jc_info, infor);
        holder.score_5.setTag(R.string.jc_info, infor);
        holder.score_6.setTag(R.string.jc_info, infor);
        holder.score_7.setTag(R.string.jc_info, infor);
        holder.score_0.setOnCheckedChangeListener(checkedChangeListener);
        holder.score_1.setOnCheckedChangeListener(checkedChangeListener);
        holder.score_2.setOnCheckedChangeListener(checkedChangeListener);
        holder.score_3.setOnCheckedChangeListener(checkedChangeListener);
        holder.score_4.setOnCheckedChangeListener(checkedChangeListener);
        holder.score_5.setOnCheckedChangeListener(checkedChangeListener);
        holder.score_6.setOnCheckedChangeListener(checkedChangeListener);
        holder.score_7.setOnCheckedChangeListener(checkedChangeListener);
        holder.delete.setTag(R.string.jc_info,infor);
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onDelete((JCInfo)(v.getTag(R.string.jc_info)));
                }
                JCBasePayActivity.infos.remove(position);
                setInfors(JCBasePayActivity.infos, isdanguan);
            }
        });
        jiSuanZhuShu();
        return convertView;
    }

    private SpannableString getShowSP(String CONTENT) {
        RelativeSizeSpan span = new RelativeSizeSpan(Constants.scaleJCTxt);
        SpannableString spannableString = new SpannableString(CONTENT);
        spannableString.setSpan(span, 0, 2, Spanned
                .SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }


    CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton
            .OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            JCInfo.SPInfo spInfo = (JCInfo.SPInfo) buttonView.getTag();
            spInfo.isSel = isChecked;
            updateTouMsg((JCInfo) buttonView.getTag(R.string.jc_info), isChecked);
        }
    };


    private void updateTouMsg(JCInfo infor, boolean isChecked) {
        if (isChecked) {
            infor.setOnclikNum(infor.getOnclikNum() + 1);
        } else {
            infor.setOnclikNum(infor.getOnclikNum() - 1);
        }
        if (infor.getOnclikNum() == 0) {
            if (listener != null) {
                listener.onDelete(infor);
            }
        }
        jiSuanZhuShu();
    }


    /**
     * 计算注数（待修改） ///苗强 ---暂不支持 多串过关  只支持 自由过关
     */
    public void jiSuanZhuShu() {

        LogUtils.e("====ZJQ jiSuanZhuShu====");

        List<Integer> clicknums = new ArrayList<>();
        List<Boolean> dannums = new ArrayList<>();

        zhuShu = 0;
        int changCi = 0;
        int wanFa = 1;
        for (int i = 0; i < infors.size(); i++) {
            JCInfo info = infors.get(i);
            if (info.getOnclikNum() != 0) {
                changCi++;
            }
            clicknums.add(info.getOnclikNum());
            if (info.isDan()) {
                wanFa = 2;
            }
            dannums.add(info.isDan());// 添加胆拖
        }

        List<Integer> chuans = new ArrayList<>();
        if (isdanguan) {
            for (int i : clicknums) {
                zhuShu += i;
            }
        } else {
            String chuan = JC_GuoGuanDialog.result;
            if (chuan.contains(",") || chuan.endsWith("串1")){
                if (chuan.contains("串")) {
                    String[] chuan_array = chuan.split(",");
                    for (int i = 0; i < chuan_array.length; i++) {
                        String chuan_i = chuan_array[i];
                        chuans.add(Integer.valueOf(chuan_i.charAt(0) + ""));
                    }

                    for (Integer i : chuans) {
                        zhuShu += JZBetNumUtil.getZhushu(i, clicknums, dannums);
                    }
                }
            } else if (chuan.contains("串")) {
                String[] split = chuan.split("串");
                int teamNum = Integer.valueOf(split[0]);
                int select = Integer.valueOf(split[1]);
//                zhuShu = JZBetNumUtil.getDuoZhushu(teamNum, select, dannums, clicknums);
//                // L场比赛选择M关的可能性为groupNum种组合情况
//                int groupNum = JZBetNumUtil.getZhushu(teamNum, clicknums, dannums);

                String betcode = JZBetContentUtil.getContentZJQ(infors, isdanguan, chuan);
                zhuShu = JZBetNumUtil.caculateZhushu(chuan, betcode, teamNum, isdanguan, dannums);

            }
        }

        Message msg = handler.obtainMessage();
        msg.obj = zhuShu;
        msg.arg1 = changCi;
        msg.arg2 = wanFa;
        msg.what = JCBasePayActivity.ZF_WHAT1;
        handler.sendMessage(msg);
    }


    class ViewHolder {
        LinearLayout zhu;
        LinearLayout delete;
        TextView team_home;
        TextView week_num;
        TextView ping1;
        TextView team_away;
        CheckBox score_0;
        CheckBox score_1;
        CheckBox score_2;
        CheckBox score_3;
        CheckBox score_4;
        CheckBox score_5;
        CheckBox score_6;
        CheckBox score_7;
        CheckBox dan;
    }

}
